// pages/orderInvoice.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum:1,
        pageSize:10,
        checkedAll: "",
        orderList:[],
        historyList:[],
        countCost:0.00,
        countOrder:0,
        isAll:false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        that.getNoFillOrderList()
        that.getNoFillHistoryList()
    },
    
    getNoFillOrderList() {
        var that = this
        wx.showLoading({
          title: '加载中...',
          mask: true
        });
        app.executeRequest(function (header) {
          wx.request({
            url: app.BASE_URL + '/invoice/noFillOrderList',
            header,
            data: {
              pageNum: that.data.pageNum,
              pageSize:that.data.pageSize
            },
            success: (res) => {
    
              var data = res.data.data
              console.log('------------->',res)

              if ((data.length>0 && data.length<10) || data.length==0) {
                that.setData({
                    isAll:true
                })
              }
              that.setData({
                orderList:that.data.orderList.concat(data)
              })
            },
            complete() {
              getApp().hideLoading()
            }
          })
    
        })
    },
    getNoFillHistoryList() {
      var that = this
      app.executeRequest(function (header) {
        wx.request({
          url: app.BASE_URL + '/invoice/fillHistoryList',
          header,
          data: {
            pageNum: that.data.pageNum,
            pageSize:that.data.pageSize
          },
          success: (res) => {
  
            var data = res.data.data
            console.log('------------->',res)
            that.setData({
              historyList:data
            })
          }
        })
  
      })
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 用户点击右上角分享
     */
    checkboxChange(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value)
        const orderList = this.data.orderList
        const values = e.detail.value
        let countCost = 0
        for (let i = 0, lenI = orderList.length; i < lenI; ++i) {
            orderList[i].checked = false
            for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
                if (orderList[i].orderCode == values[j]) {
                    orderList[i].checked = true
                    countCost = countCost + parseFloat(orderList[i].fee.split('元')[0])
                    break
                }
            }
        }
        this.setData({
            orderList
        })
        console.log(orderList)
        if (e.detail.value.length == orderList.length) {
            console.log(this.data.checkedAll);
            this.setData({
                checkedAll: true
            })
        }else{
            this.setData({
                checkedAll: ""
            })
        }
        this.setData({
            countCost:countCost.toFixed(2),
            countOrder:values.length
        })

    },
    checkboxAll(e) {
        let countCost = 0
        if (e.detail.value.length == 1) {
            // 全选状态
            const orderList = this.data.orderList
            for (let i = 0; i < orderList.length; i++) {
                orderList[i].checked = true
                countCost = countCost + parseFloat(orderList[i].fee.split('元')[0])

            }
            this.setData({
                orderList
            })
        } else {
            // 没有全选状态
            const orderList = this.data.orderList
            for (let i = 0; i < orderList.length; i++) {
                orderList[i].checked = false
            }
            this.setData({
                orderList
            })
        }
        console.log(countCost)
        let countOrder=0
        if(e.detail.value.length){
          countOrder= this.data.orderList.length?this.data.orderList.length:0
        }
        this.setData({
            countCost:countCost.toFixed(2),
            countOrder:countOrder
        })
    },
    submit() {
        const checkList = this.data.orderList.map(item =>{ if (item.checked) { return item.orderCode}})
        wx.navigateTo({
          url: '../../pkgBak/pages/invoiceForm/invoiceForm?checkList='+checkList.toString()+'&invoiceCost='+this.data.countCost,
        })
        
        // 待开发
    },

    loadMore() {
        if(!this.data.isAll) {
            this.setData({
                pageNum:this.data.pageNum+1
            })
            this.getNoFillOrderList()
        }
    },
    historyTip() {
        wx.navigateTo({
          url: '../../pkgBak/pages/invoiceHistroy/invoiceHistroy',
        })
    }
    
})